package com.gomore.opple.data.local;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.gomore.opple.rest.employee.LoginResult;
import com.gomore.opple.utils.JacksonUtils;
import com.gomore.opple.web.cgform.consumer.entity.TOConsumerEntity;

import java.util.List;

public class PreferencesManager implements PreferencesApi {
    private static PreferencesManager mPreferencesManager;

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;
    private final static String PREFERENCE_NAME = "car_preferences";

    private static final String USERNAME = "userName";
    private static final String PASSWORD = "passWord";
    private static final String USER = "user";
    private static final String CONSUMER = "consumer";
    private static final String APPVERSION = "appVersion";
    private static final String SEARCHKEY = "searchKey";

    private PreferencesManager(Context context) {
        initSharedPreferences(context);
    }

    private void initSharedPreferences(Context context) {
        mSharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public static synchronized PreferencesManager getInstance(Context context) {
        if (mPreferencesManager == null) {
            mPreferencesManager = new PreferencesManager(context);
        }
        return mPreferencesManager;
    }

    /**
     * @param username 用户名
     */
    @Override
    public void saveUserName(String username) {
        mEditor.putString(USERNAME, username).commit();
    }

    /**
     * @return
     */
    @Override
    public String getUserName() {
        String userName = mSharedPreferences.getString(USERNAME, "");
        if (userName != null) {
            return userName;
        }
        return null;
    }

    /**
     * @param passWord 用户密码
     */
    @Override
    public void savePassWord(String passWord) {
        mEditor.putString(PASSWORD, passWord).commit();
    }

    /**
     * @return
     */
    @Override
    public String getPassWord() {
        String userName = mSharedPreferences.getString(PASSWORD, "");
        if (userName != null) {
            return userName;
        }
        return null;
    }


    /**
     * 保存用户信息
     */
    @Override
    public void saveUser(LoginResult user) {
        if (user != null) {
            String userStr = JacksonUtils.toJson(user);
            mEditor.putString(USER, userStr).commit();
        }
    }

    /**
     * 获取用户信息
     */
    @Override
    public LoginResult getUser() {
        try {
            String userStr = mSharedPreferences.getString(USER, "");
            if (!TextUtils.isEmpty(userStr)) {
                return JacksonUtils.fromJson(userStr, LoginResult.class);
            }
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public void saveConsumer(TOConsumerEntity toConsumerEntity) {
        if (toConsumerEntity != null) {
            String userStr = JacksonUtils.toJson(toConsumerEntity);
            mEditor.putString(CONSUMER, userStr).commit();
        }
    }

    @Override
    public TOConsumerEntity getConsumer() {
        try {
            String userStr = mSharedPreferences.getString(CONSUMER, "");
            if (!TextUtils.isEmpty(userStr)) {
                return JacksonUtils.fromJson(userStr, TOConsumerEntity.class);
            }
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public void clearConsumer() {
        mEditor.remove(CONSUMER);
        mEditor.commit();
    }

    /**
     * 保存版本号
     *
     * @param version
     */
    @Override
    public void saveAppVersion(String version) {
        mEditor.putString(APPVERSION, version).commit();
    }

    /**
     * 获取版本号
     *
     * @return
     */
    @Override
    public String getAppVersion() {
        String appVersion = mSharedPreferences.getString(APPVERSION, "");
        if (appVersion != null) {
            return appVersion;
        }
        return null;
    }

    @Override
    public void clearMessage() {
        mEditor.clear();
        mEditor.commit();
    }

    /**
     * 获取搜索关键字
     *
     * @return
     */

    @Override
    public List<String> getSearchKey() {
        try {
            String userStr = mSharedPreferences.getString(SEARCHKEY, "");
            if (!TextUtils.isEmpty(userStr)) {
                return JacksonUtils.fromJson(userStr, List.class);
            }
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 保存搜索关键字
     *
     * @param keys
     */
    @Override
    public void saveSearchKey(List<String> keys) {
        if (keys != null) {
            String key = JacksonUtils.toJson(keys);
            mEditor.putString(SEARCHKEY, key).commit();
        }
    }

    /**
     * 清空历史记录
     */
    @Override
    public void clearHistory() {
        mEditor.remove(SEARCHKEY);
        mEditor.commit();
    }

}
